package gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import utils.DateUtils;

import com.toedter.calendar.JDateChooser;

import java.awt.Font;
import net.miginfocom.swing.MigLayout;


@SuppressWarnings("serial")
public class DateWindow extends JFrame implements ActionListener{

	private static String title = "Domandino - Date";
	private static JPanel centerPanel = new JPanel();
	private static JPanel northPanel = new JPanel();
	private static JPanel southPanel = new JPanel();
	private static JDateChooser datePicker = new JDateChooser();
	private static JButton button = new JButton("ok");
	private static JLabel text = new JLabel();
	private static Object lock = new Object();
	
	public DateWindow(){
		
		super(title);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container cp = this.getContentPane();
		getContentPane().setLayout(new MigLayout("", "[10:n][][10:n]", "[10:n][][][][10:n]"));
		text.setFont(new Font("Dialog", Font.BOLD, 14));
		
		text.setText("Inserisci la data dell'esame");
		northPanel.add(text);
		cp.add(northPanel, "cell 1 1,alignx left,aligny top");
		
		
		datePicker.setMinSelectableDate(new Date());
		centerPanel.add(datePicker);
		cp.add(centerPanel, "cell 1 2,grow");
		button.setFont(new Font("Dialog", Font.BOLD, 14));
		
		button.addActionListener(this);
		southPanel.add(button);
		cp.add(southPanel, "cell 1 3,growx,aligny top");
		
		pack();
		
	}

	
	@Override
	public void actionPerformed(ActionEvent ae) {
		// TODO Auto-generated method stub
		Date examDate= datePicker.getDate();
		if(examDate == null){
			text.setText("Inserisci una data valida");
			text.setForeground(Color.RED);
			text.repaint();
		}else{
				try {
					DateUtils.saveDate(datePicker.getDate());
					this.dispose();
					synchronized (lock) {
						lock.notify();
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
		}
	}
	public void waitFor(){
		synchronized (lock) {
			try {
				lock.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	
}